import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class TreeWidgetDemo(QMainWindow):
    def __init__(self,parent=None):
        super(TreeWidgetDemo,self).__init__(parent)
        self.setWindowTitle('treeWdiget案例')
        self.tree=QTreeWidget()
        #设置列数
        self.tree.setColumnCount(2)
        #设置标题
        self.tree.setHeaderLabels(['key','value'])
        #设置根节点
        root=QTreeWidgetItem(self.tree)
        root.setText(0,"root")
        root.setIcon(0,QIcon('panda.jpg'))
        #设置列宽
        self.tree.setColumnWidth(0,160)
        child1=QTreeWidgetItem(root)
        child1.setText(0,'child1')
        child1.setText(1,'python')
        child1.setCheckState(0,Qt.Checked)
        child2 = QTreeWidgetItem(root)
        child2.setText(0, 'child2')
        child2.setText(1, 'java')
        child2.setCheckState(0, Qt.Checked)

        child3 = QTreeWidgetItem(root)
        child3.setText(0, 'child3')
        child3.setText(1, 'js')
        child3.setCheckState(0, Qt.Checked)
        self.tree.addTopLevelItem(root)

        #全部展开
        self.tree.expandAll()
        self.setCentralWidget(self.tree)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    tree=TreeWidgetDemo()
    tree.show()
    sys.exit(app.exec_())
# if __name__ == '__main__':
#     app=QApplication(sys.argv)
#     model=QDirModel()
#     tree=QTreeView()
#     tree.setModel(model)
#     tree.setWindowTitle("tree")
#     tree.resize(600,400)
#     tree.show()
#     sys.exit(app.exec_())